home *** CD-ROM | disk | FTP | other *** search
- Path: galaxy.ucr.edu!not-for-mail
- From: thp@cs.ucr.edu (Tom Payne)
- Newsgroups: comp.lang.c++
- Subject: Re: Newbie Q: references
- Date: 24 Feb 1996 19:59:38 GMT
- Organization: University of California, Riverside Department of Computer Science
- Message-ID: <4gnqna$5hv@galaxy.ucr.edu>
- References: <4fegaq$gvc@dns.plano.net> <4figeu$24l@news1.usa.pipeline.com>
- NNTP-Posting-Host: corvette.ucr.edu
- X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
-
- grantp@usa.pipeline.com wrote:
- [...]
- : No. Most implementations pass the reference the same way as a
- : pointer. In other words, the only difference between a pointer
- : and a reference to an object is the semantics in the language.
-
- Once they are intialized references behave exactly like pointers,
- except that they are automatically dereferenced -- think of a pointer
- with an invisible "*" in front of it. (As a consequence, you can't
- modify (reinitialize) references, because all attempts to do so will
- be referred to the referent.)
-
- Regarding initialization, there is the difference that references can
- be initialized to refer to an expression, in which case a temporary is
- created to hold that expression. Also, when initialized by the
- catching of an exception, references and pointers behaved in vastly
- different ways -- see "More Effective C++" by Scott Meyers for
- details.
-
- As types, there are significant limitations on how references can be
- used in building further types, e.g., arrays of references are out, as
- are pointers to references. There are a couple of other quirks as
- well. Reference members destroy the "PODness" of a struct, freeing it
- from the constraint of C-compatible layout. Also, we have the
- absurdity that sizeof(Widget&) is sizeof(Widget) regardless of the
- number of bytes used to encode references. (The April draft standard
- doesn't say this but members standards committee assure me its what
- they intend, and it is how they've written their compilers.)
-
- One can have delightful metaphysical arguments about whether or not
- references are objects, or simply "aliases." We know from first
- principles that they occupy run-time data storage, which is the
- definition of what it means to be an "object." But, if you try to
- find the address of that storage, references being what they are,
- you'll only get the address of the referrent. Some argue that, if you
- can't find its address, it doesn't have one, and. if it doesn't have
- one, it's not an object. (If a tree falls in the forest and no one
- hears it ... )
-
- References are not always aliases, since they can refer to nameless
- objects, as does the intger reference returned by
- int& f(){ return * new int; }
- and aliases are names, which are uses of identifiers.
-
-
- Tom Payne (thp@cs.ucr.edu)
-